package my.xh.validate.handler.impl;

import my.xh.validate.ValidateField;
import my.xh.validate.handler.ValidateHandler;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName RegStrValidateHandler
 * @Date 2019/1/17 15:20
 * @Author xuanhu
 * @Description 正则校验
 */
@Component
public class RegStrValidateHandler implements ValidateHandler {
    @Override
    public boolean validate(ValidateField validateField, StringBuilder paramName, Object paramValue, Map<String, Object> map, boolean flag) {
        if (!"".equals(validateField.regStr())) { // 判断正则
            if (paramValue instanceof String) {
                if (!((String) paramValue).matches(validateField.regStr())) {
                    flag = false;
                    map.put(paramName.toString(), getMsg(validateField, paramName.append(" 不能配匹该正则：").append(validateField.regStr()).toString()));
                }
            } else {
                flag = false;
                map.put(paramName.toString(), getMsg(validateField, paramName.append(" 必须为String类型").toString()));
            }
        }
        return flag;
    }
}
